package com.qianli.cashmama.product.server.facade;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Response;
import com.fqgj.common.api.enums.BasicErrorCodeEnum;
import com.qianli.cashmama.product.facade.AppFacade;
import com.qianli.cashmama.product.facade.result.AppConfig;
import com.qianli.cashmama.product.facade.result.AppPackageInfo;
import com.qianli.cashmama.product.facade.result.AppProductCategoryVo;
import com.qianli.cashmama.product.facade.result.AppVo;
import com.qianli.cashmama.product.server.dao.AppDao;
import com.qianli.cashmama.product.server.dao.AppPackageInfoDao;
import com.qianli.cashmama.product.server.dao.AppProductCategoryDao;
import com.qianli.cashmama.product.server.entity.AppEntity;
import com.qianli.cashmama.product.server.entity.AppPackageInfoEntity;
import com.qianli.cashmama.product.server.entity.AppProductCategoryEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/9/25
 * Time: 上午11:18
 */
@Service("appFacade")
public class AppFacadeImpl implements AppFacade {

    @Autowired
    private AppDao appDao;
    @Autowired
    private AppPackageInfoDao appPackageInfoDao;
    @Autowired
    private AppProductCategoryDao appProductCategoryDao;

    /**
     * 根据app的编码查询
     *
     * @param appCode app的编码
     * @return
     */
    @Override
    public Response<AppVo> getAppByAppCode(String appCode) {
        AppEntity entity = appDao.selectAppByAppCode(appCode);
        if (entity == null) {
            return Response.ok();
        }
        AppVo appVo = new AppVo()
                .setAppCode(entity.getAppCode())
                .setAppName(entity.getAppName())
                .setDisplayName(entity.getDisplayName());
        if (StringUtils.isNotEmpty(entity.getConfig())) {
            appVo.setConfig(JSON.parseObject(entity.getConfig(), AppConfig.class));
        }
        return Response.ok(appVo);
    }

    @Override
    public Response<List<AppVo>> getAllApps() {
        List<AppEntity> entityList = appDao.getAllApps();
        if (entityList == null) {
            return Response.ok(new ArrayList<>());
        }
        List<AppVo> response = entityList.stream()
                .map(appEntity -> new AppVo().setAppCode(appEntity.getAppCode()).setAppName(appEntity.getAppName()))
                .collect(Collectors.toList());
        return Response.ok(response);
    }

    @Override
    public Response<AppPackageInfo> getAppPackageInfoByPackageName(String packageName) {
        if (StringUtils.isBlank(packageName)) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "packageName invalid");
        }
        AppPackageInfoEntity entity = appPackageInfoDao.getAppPackageInfoByPackageName(packageName);
        if (entity == null) {
            return Response.ok().putData(null);
        }
        AppPackageInfo appPackageInfo = new AppPackageInfo();
        BeanUtils.copyProperties(entity, appPackageInfo);
        if (StringUtils.isNotEmpty(entity.getConfig())) {
            appPackageInfo.setConfig(JSON.parseObject(entity.getConfig(), AppConfig.class));
        }
        return Response.ok().putData(appPackageInfo);
    }

    @Override
    public Response<List<AppProductCategoryVo>> getAllAppProductCategory() {
        Map<String, Object> params = new HashMap<>();
        params.put("isDeleted", 0);
        List<AppProductCategoryEntity> entityList = appProductCategoryDao.selectByParams(params);
        if (entityList == null) {
            return Response.ok();
        }
        List<AppProductCategoryVo> list = entityList.stream()
                .map(entity -> new AppProductCategoryVo()
                        .setAppCode(entity.getAppCode())
                        .setProductCategory(entity.getProductCategory())
                        .setAppName(entity.getAppName())
                ).collect(Collectors.toList());
        return Response.ok().putData(list);
    }
}
